﻿package bdz.projects.lavender.doi.ui.core
{
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.utils.*;
	import bdz.projects.lavender.doi.events.DOIEvent;
	/**
	 * ...
	 * @author ...
	 */
	public class DOIListView extends MovieClip 
	{
		protected var _items:Array;
		protected var _dp:Array;
		protected var _renderer:String;
		private var _inited:Boolean = false;
		function DOIListView() {
			addEventListener(Event.ADDED_TO_STAGE, initializeComponent);
		}
		protected function initializeComponent(evt:Event):void {
			if (_dp) {
				render();
			}
			
		}
		protected function clearItems():void {
			for each(var o:* in _items) {
				try {
					removeChild(o);
				}catch (e:Error) {
				}
			}
		}
		protected function layoutViews():void {
			clearItems();
		}
		protected function onItemClicked(evt:DOIEvent):void {
			dispatchEvent(new DOIEvent(DOIEvent.ITEM_CLICKED, {ui:evt.target, info:evt.target.data}));
		}
		protected function render():void {
			_items = new Array();
			var counter:Number = 0;
			for each(var obj:Object in _dp) {
				var rend:Object = getDefinitionByName(_renderer);
				var ref:* = new rend();
				ref.data = obj;
				ref.index = counter;
				ref.addEventListener(DOIEvent.ITEM_CLICKED, onItemClicked);
				_items.push(ref);
				counter++;
			}
			layoutViews();
		}
		public function set renderer(r:String):void {
			_renderer = r;
		}
		public function get renderer():String {
			return _renderer;
		}
		public function get dataProvider():Array {
			return _dp;
		}
		public function set dataProvider(arr:Array):void {
			_dp = arr;
			
		}
		
	}
	
}